package com.terrace.core.mybatis.plugin.pagination.parser;

import java.util.Map;

import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;

import com.terrace.core.mybatis.entity.Pagination;

/**
 * @author liuzh
 */
public class OracleParser extends AbstractParser {

	@Override
	public String getPaginationSQL(String sql) {
		StringBuilder sqlBuilder = new StringBuilder(sql.length() + 120);
		sqlBuilder.append("SELECT * FROM ( SELECT TMP_PAGE.*, ROWNUM ROW_ID FROM (");
		sqlBuilder.append(sql);
		sqlBuilder.append(") _SELECT_PAGINATION WHERE ROWNUM<=?) WHERE ROW_ID>?");
		return sqlBuilder.toString();
	}

	@Override
	public Map<String, Object> setPageParameter(MappedStatement ms, Object parameterObject, BoundSql boundSql, Pagination<?> page) {
		Map<String, Object> paramMap = super.setPageParameter(ms, parameterObject, boundSql, page);
		paramMap.put(PAGEPARAMETER_FIRST, page.getEndRow());
		paramMap.put(PAGEPARAMETER_SECOND, page.getStartRow());
		return paramMap;
	}

}